//
//  JoiningViewController.swift
//  jiahui
//
//  Created by 3tee on 2024/11/26.
//

import UIKit
import Moya
import RxCocoa
import RxSwift

class JoiningViewController: UIViewController {

    var loginModel: LoginModel?
    @IBOutlet weak var roomTf: UITextField!
    @IBOutlet weak var videoSwitch: UISwitch!
    @IBOutlet weak var audioSwitch: UISwitch!
    @IBOutlet weak var nameTf: UITextField!
    @IBOutlet weak var joinBtn: UIButton!
    
  

    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "加入会议"
        let disposeBag = DisposeBag()
        self.navigationController?.delegate = self
        // Do any additional setup after loading the view.
        //reqRoomToken()//接口已经弃用404
        videoSwitch.setOn(UserDefaultsPlugin.bool(forKey: Constant.key_video), animated: true)
        audioSwitch.setOn(UserDefaultsPlugin.bool(forKey: Constant.key_audio), animated: true)
    }
    override func viewWillAppear(_ animated: Bool) {
        if let model = loginModel {
            roomTf.text = model.room_id
            nameTf.text = model.name
        }
    }
    
    func reqRoomToken(){
        let provider = MoyaProvider<RestAPI>(plugins:[NetworkLoggerPlugin()])
        provider.request(.getRoomToken(name: "zqj", roomId: "12345678980")) { result in
            switch result{
            case .success(let response):
                print("获取roomtoken code = \(response.statusCode)")
            case .failure(let err):
                print("获取roomtoken 失败")
            }
        }
    }

    @IBAction func go2meeting(_ sender: UIButton) {
       
        let videoOn = UserDefaultsPlugin.bool(forKey: Constant.key_video)
        let audioOn = UserDefaultsPlugin.bool(forKey: Constant.key_audio)

        if let roomId = roomTf.text,let userName = nameTf.text {
            ThreeTManager.shardInstance.joinRoom(roomId, userName, userid:String(describing: loginModel?.user_id),videoOn: videoOn,audioOn: audioOn) { isSuccess in
                print("加入房间成功")
                let meetingvc = MeetingViewController()
                self.navigationController?.pushViewController(meetingvc, animated: true)
            }
        }
//        meetingvc.modalPresentationStyle = .overFullScreen
//         present(meetingvc, animated: true)
    }
    
 
}

extension JoiningViewController: UINavigationControllerDelegate{
    func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> (any UIViewControllerAnimatedTransitioning)? {
        PushAnimation()
    }
}
